/****************************************************************************
*                                                                           *
* simarea.cpp	 															*
*                                                                           *
* ALaRI 2008												                *
*                                                                           *
****************************************************************************/


/****************************************************************************
* Includes										                            *
****************************************************************************/
#include "simarea.h" // me
#include "../../common/property.h"
#include <QPainter>
#include <QPoint>
#include <QPaintEvent>
#include <QMouseEvent>

QSimWidget::QSimWidget(QWidget *pParent /* = NULL */)
	: QWidget(pParent),
	m_pSensor(NULL)
{
	setToolTip(tr("Simulated area"));
}

void QSimWidget::mouseMoveEvent(QMouseEvent *pEvent)  
{
	int nX = pEvent->x();
	int nY = pEvent->y();

	if (m_pSensor == NULL)
	{
		setToolTip(tr(""));
		return;
	}
	QPoint p = pEvent->pos() - m_pSensor->point();
	unsigned char byValue = m_pSensor->getFieldAt(nX, nY);
	if (m_pSensor != NULL)
		setToolTip(QString("-%1dB@%2cm").arg(byValue).arg(p.manhattanLength()*CSimProperty::inst()->getResolution()));
	else setToolTip(tr(""));
}

void QSimWidget::drawImage(const QImage& image)
{
	m_qImage = image;
	QRect rect = m_qImage.rect();
	resize(rect.width(), rect.height());
	repaint();
	setMouseTracking(true);
}

void QSimWidget::clear()
{
	m_qImage = QImage();
	m_pSensor = NULL;
}

void QSimWidget::resize(int nWidth, int nHeight)
{
	setBaseSize(QSize(nWidth, nHeight));
	setMinimumSize(nWidth, nHeight);
	setMaximumSize(nWidth, nHeight);
}

void QSimWidget::selectSensor(CSensor *pSensor)
{
	assert(NULL != pSensor);
	m_pSensor = pSensor;
	repaint();
}

void QSimWidget::paintEvent(QPaintEvent *pEvent)
{
	if (NULL != m_pSensor)
	{
		QPainter painter(this);
		painter.drawImage(QPoint(0, 0), m_qImage);
	}
}
